package com.wzq.solution;

import java.util.Arrays;
import java.util.Comparator;

class Solution56 {
    public static int[][] merge(int[][] intervals) {
        int len = intervals.length;
        if(len < 2){
            return intervals;
        }
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        int j=0;
        for (int i=1;i<len;i++){
            if(intervals[i][0] > intervals[j][1]){
                intervals[++j] = intervals[i];
            }else{
                if(intervals[i][1] > intervals[j][1]){
                    intervals[j][1] = intervals[i][1];
                }
            }
        }

        return Arrays.copyOf(intervals, j+1);
    }

    public static void main(String[] args) {
        int[][] a = new int[][]{{2,3},{4,5},{6,7},{8,9},{1,10}};

        a = merge(a);
        System.out.println();
    }
}